import type { QmsIqc, QmsIqcDetail, QmsIqcRecord } from "@/pages/qms/iqc/entity";
import apiClient from "../../apiClient";

export interface PageListReq {
  //来料检验单编号
  code?: string;
  //来源单据类型
  sourceType?: string;
  //来源单据编号
  sourceCode?: string;
  //产品物料ID
  materialId?: number;
  //检测结果
  checkResult?: string;
  //当前页码
  page: number;
  //每页数量
  pageSize: number;
}

export interface PageListRes {
  //总记录数
  total: number;
  //数据列表
  records: QmsIqc[];
}

const pageList = (data: PageListReq): Promise<PageListRes> =>
  apiClient.post({
    url: "/qms/iqc/pageList",
    data
});
const getInfo = (id: React.Key): Promise<QmsIqc> =>
  apiClient.get({
    url: `/qms/iqc/${id}`
});

const add = (data: QmsIqc): Promise<QmsIqc> =>
  apiClient.post({
    url: "/qms/iqc",
    data
});

const update = (data: QmsIqc): Promise<QmsIqc> =>
  apiClient.put({
    url: "/qms/iqc",
    data
});

const remove = (ids: React.Key[]): Promise<void> =>
  apiClient.delete({
    url: "/qms/iqc",
    params: {
    ids: ids.join(",")
  }
});

const getIqcDetails = (id: React.Key): Promise<QmsIqcDetail[]> =>
  apiClient.post({
    url: "/qms/iqc/detail/list",
    data: {
      iqcId: id
    }
  });

const getIqcRecords = (id: React.Key): Promise<QmsIqcRecord[]> =>
  apiClient.post({
    url: "/qms/iqc/record/list",
    data: {
      iqcId: id
    }
  });  

const saveIqcRecord = (data:any) => {
  return apiClient.post({
    url: "/qms/iqc/record/batchSave",
    data
  });
}

export const qmsIqcService = {
  pageList,
  getInfo,
  add,
  update,
  remove,
  getIqcDetails,
  getIqcRecords,
  saveIqcRecord
};

export default qmsIqcService;